---
title: "Server Configuration | Server & DB"
description: "Configure the Mastra server with custom settings for port, timeout, CORS, and more."
---

# Server Configuration

When deploying your Mastra application to production, it runs as an HTTP server that exposes your agents, workflows, and other functionality as API endpoints. This page covers how to configure and customize the server for a production environment.

:::info

This page covers the `server` configuration options passed to the `Mastra` constructor. For running Mastra with your own HTTP server (Hono, Express, etc.), see [Server Adapters](/docs/v1/server-db/server-adapters).

:::

## Server architecture

Mastra uses [Hono](https://hono.dev) as its underlying HTTP server framework. When you build a Mastra application using `mastra build`, it generates a Hono-based HTTP server in the `.mastra` directory.

The server provides:

- API endpoints for all registered agents
- API endpoints for all registered workflows
- Custom API route support
- Custom middleware support
- Configuration of timeout
- Configuration of port
- Configuration of body limit

See the [Middleware](/docs/v1/server-db/middleware) and
[Custom API Routes](/docs/v1/server-db/custom-api-routes) pages for details on
adding additional server behaviour.

## Server configuration

You can configure server `port` and `timeout` in the Mastra instance.

```typescript title="src/mastra/index.ts" copy showLineNumbers
import { Mastra } from "@mastra/core";

export const mastra = new Mastra({
  // ...
  server: {
    port: 3000, // Defaults to 4111
    timeout: 10000, // Defaults to 3 * 60 * 1000 (3 minutes)
  },
});
```

## TypeScript configuration

Mastra requires `module` and `moduleResolution` values that support modern Node.js versions. Older settings like `CommonJS` or `node` are incompatible with Mastra’s packages and will cause resolution errors.

```json {4-5} title="tsconfig.json" copy
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ES2022",
    "moduleResolution": "bundler",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "noEmit": true,
    "outDir": "dist"
  },
  "include": ["src/**/*"]
}
```

> This TypeScript configuration is optimized for Mastra projects, using modern module resolution and strict type checking.

## CORS configuration

Mastra allows you to configure CORS (Cross-Origin Resource Sharing) settings for your server.

```typescript title="src/mastra/index.ts" copy showLineNumbers
import { Mastra } from "@mastra/core";

export const mastra = new Mastra({
  // ...
  server: {
    cors: {
      origin: ["https://example.com"], // Allow specific origins or '*' for all
      allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
      allowHeaders: ["Content-Type", "Authorization"],
      credentials: false,
    },
  },
});
```

## Stream data redaction

When streaming agent responses, the HTTP streaming layer redacts system prompts, tool definitions, API keys, and similar data from each chunk before sending it to clients. This is enabled by default.

If you're using server adapters directly, you can configure redaction behavior. See [Stream data redaction](/docs/v1/server-db/server-adapters#stream-data-redaction) in the Server Adapters docs.
